package it.yuzuojian.com.util;

import com.alibaba.druid.pool.DruidDataSource;
import it.yuzuojian.com.controller.GenerateController;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class DataSourceUtil {
    DruidDataSource dataSource = null;
    public static String resourcesDirectory;
    public static String sourceDirectory;

    public DataSource getDataSource() throws IOException {
        if(dataSource!=null){
            return dataSource;
        }
        InputStream inputStream = new FileInputStream(resourcesDirectory+ File.separator+"generator.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(properties.getProperty("url"));
        dataSource.setPassword(properties.getProperty("password"));
        dataSource.setDriverClassName(properties.getProperty("driverClassName"));
        dataSource.setUsername(properties.getProperty("username"));
        return dataSource;
    }

    public JdbcTemplate getJdbcTemplate() throws IOException {
        return new JdbcTemplate(getDataSource());
    }


}
